package com.gitee.wsl.compose.draw.sketch.noise

import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.PointMode
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.unit.IntSize
import com.gitee.wsl.common.ui.base.Vector2D
import com.gitee.wsl.common.ui.base.limit
import com.gitee.wsl.common.ui.base.plusAssign
import com.gitee.wsl.common.ui.base.setMag
import com.gitee.wsl.common.ui.base.timesAssign
import com.gitee.wsl.common.ui.base.toOffSet
import com.gitee.wsl.compose.draw.sketch.k5
import com.gitee.wsl.math.random.RandomExt.k5Random
import com.gitee.wsl.math.Math
import com.gitee.wsl.math.Noise.noise2D


var inc = 0.1f
var scl = 10

data class Particle(val dimens: IntSize) {

    val position = Vector2D(k5Random(max = dimens.width), k5Random(max = dimens.height))
    val velocity = Vector2D(0f, 0f)
    val acceleration = Vector2D(0f, 0f)
    val maxSpeed = 4f
    var skipPoint = false
    val points = mutableListOf(position.toOffSet())

    fun update() {
        velocity += acceleration
        velocity.limit(maxSpeed)
        position += velocity
        acceleration *= 0f
    }

    fun follow(force: Vector2D) {
        val x = position.x / scl
        val y = position.y / scl
        applyForce(force)
    }

    private fun applyForce(force: Vector2D) {
        acceleration += force
    }

    fun render(drawScope: DrawScope) {
        points.add(position.toOffSet())
        drawScope.drawPoints(points, pointMode = PointMode.Polygon, Color.White)
    }

    fun edges() {
        skipPoint = false
        if (position.x > dimens.width) {
            position.x = 0f
            skipPoint = true
        }
        if (position.x < 0) {
            position.x = dimens.width.toFloat()
            skipPoint = true
        }
        if (position.y > dimens.height) {
            position.y = 0f
            skipPoint = true
        }
        if (position.y < 0) {
            position.y = dimens.height.toFloat()
            skipPoint = true
        }
    }
}

@Composable
fun simplexNoise() = k5 {

    val particle = Particle(dimensInt)
    var yoff = 0f
    var xoff = 0f
    onDraw { drawScope ->
        val angle = noise2D(x = xoff.toDouble(), y = yoff.toDouble()) * Math.TWO_PI + 4
        val v = Vector2D.fromAnAngle(angle.toFloat())
        v.setMag(1f)
        xoff += inc
        yoff += inc

        particle.apply {
            follow(v)
            update()
            // it.edges()
            render(drawScope)
        }
    }
}
